#include "../include/main.h"
#include "./common.c"

#define GAME_OVER 1
#define SLEEP_TIME_MS 500

const int MAX_ROW = 10;
const int MAX_COLUMN = 30;

const int BIRD_INIT_ROW = 4;
const int BIRD_INIT_COLUMN = 14;
const int GAME_OVER_INIT = 0;
const int BIRD_FLY_HIGH = 2;

struct BirdStruct{
    int x;
    int y;
    int gameOver;
    int score;
};

int main(){
    int sleep_ms = 0;
    sleep_ms = SLEEP_TIME_MS;

    //关闭回显
    set_disp_mode(STDIN_FILENO,0);

    //初始化鸟
    struct BirdStruct bird;
    bird.x = BIRD_INIT_ROW;
    bird.y = BIRD_INIT_COLUMN;
    bird.gameOver = GAME_OVER_INIT;
    bird.score = 0;

    //打印背景
    char background_char[MAX_ROW][MAX_COLUMN] = {
        {'x', 'x', 'x',' ', ' ', ' ','x', 'x', 'x',' ', ' ', ' ','x', 'x', 'x',' ', ' ', ' ','x', 'x', 'x',' ', ' ', ' ', 'x', 'x','x', ' ', ' ', ' '},
        {'x', 'x', 'x',' ', ' ', ' ','x', 'x', 'x',' ', ' ', ' ','x', 'x', 'x',' ', ' ', ' ','x', 'x', 'x',' ', ' ', ' ', 'x', 'x','x', ' ', ' ', ' '},
        {'x', 'x', 'x',' ', ' ', ' ',' ', ' ', ' ',' ', ' ', ' ',' ', ' ', ' ',' ', ' ', ' ','x', 'x', 'x',' ', ' ', ' ', 'x', 'x','x', ' ', ' ', ' '},
        {' ', ' ', ' ',' ', ' ', ' ',' ', ' ', ' ',' ', ' ', ' ',' ', ' ', ' ',' ', ' ', ' ',' ', ' ', ' ',' ', ' ', ' ', 'x', 'x','x', ' ', ' ', ' '},
        {' ', ' ', ' ',' ', ' ', ' ',' ', ' ', ' ',' ', ' ', ' ',' ', ' ', ' ',' ', ' ', ' ',' ', ' ', ' ',' ', ' ', ' ', ' ', ' ',' ', ' ', ' ', ' '},
        {' ', ' ', ' ',' ', ' ', ' ',' ', ' ', ' ',' ', ' ', ' ',' ', ' ', ' ',' ', ' ', ' ',' ', ' ', ' ',' ', ' ', ' ', ' ', ' ',' ', ' ', ' ', ' '},
        {' ', ' ', ' ',' ', ' ', ' ','x', 'x', 'x',' ', ' ', ' ','x', 'x', 'x',' ', ' ', ' ',' ', ' ', ' ',' ', ' ', ' ', ' ', ' ',' ', ' ', ' ', ' '},
        {'x', 'x', 'x',' ', ' ', ' ','x', 'x', 'x',' ', ' ', ' ','x', 'x', 'x',' ', ' ', ' ','x', 'x', 'x',' ', ' ', ' ', ' ', ' ',' ', ' ', ' ', ' '},
        {'x', 'x', 'x',' ', ' ', ' ','x', 'x', 'x',' ', ' ', ' ','x', 'x', 'x',' ', ' ', ' ','x', 'x', 'x',' ', ' ', ' ', 'x', 'x','x', ' ', ' ', ' '},
        {'x', 'x', 'x',' ', ' ', ' ','x', 'x', 'x',' ', ' ', ' ','x', 'x', 'x',' ', ' ', ' ','x', 'x', 'x',' ', ' ', ' ', 'x', 'x','x', ' ', ' ', ' '}
    };
    
    for (int i = 0; i < 500; i++)
    {
        CLEAR_SCREEN;

        //轮询获取键盘输入，这个方法有点东西
        if(kbhit() == 1){
            int c;
            
            c = getchar();//输入abc
            // set_disp_mode(STDIN_FILENO,1);
            if (c == 10)//回车
            {
                bird.x -= BIRD_FLY_HIGH;
            }
        }else{
            // printf("no input \n");
        }

        //滚动背景，让第一列的数据到最后一列去，其他列向前移动一个位置
        for (int i = 0; i < MAX_ROW; i++)
        {
            char temp;
            temp = ' ';
            for (int j = 0; j < MAX_COLUMN; j++)
            {
                //滚动背景前清除鸟的位置
                if (background_char[i][j] == '&')
                {
                    background_char[i][j] = ' ';
                }
                
                if (j == 0)
                {
                    temp = background_char[i][j];
                }else if (j == MAX_COLUMN - 1)
                {
                    background_char[i][j - 1] = background_char[i][j];
                    background_char[i][j] = temp;
                }else{
                    background_char[i][j - 1] = background_char[i][j];
                }
                
            }
            
        }

        //放置鸟的位置，并且判断是否被撞击
        if (bird.x >= MAX_ROW || bird.x < 0)
        {
            bird.gameOver = GAME_OVER;//这种情况就不画鸟的位置了，防止数组越界
        }else if ( background_char[bird.x][bird.y] == 'x')
        {
            bird.gameOver = GAME_OVER;
            background_char[bird.x][bird.y] = '&';
        }else{
            background_char[bird.x][bird.y] = '&';
        }
        
        //打印背景和鸟
        for (int i = 0; i < MAX_ROW; i++)
        {
            for (int j = 0; j < MAX_COLUMN; j++)
            {
                printf("%c", background_char[i][j]);
            }
            printf("\n");
        }

        //是否game over
        if (bird.gameOver == GAME_OVER)
        {
            printf("\n\n  GAME OVER ! \n your socre is %d!\n\n", bird.score);
            set_disp_mode(STDIN_FILENO, 1);
            return 0;
        }

        bird.x++;//x位移就是积分情况
        // bird.y++;
        bird.score++;
        
        //根据时长调整鸟的飞行速度，实际是北京的滚动速度
        sleep_ms -= 5;
        
        common_sleep(sleep_ms);
    }
    
    //开启回显
    set_disp_mode(STDIN_FILENO, 1);
}